package com.wwwarehouse.financialcenter.fragment.moneywarehouse.bills;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.SpaceItemDecoration;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.adapter.moneyhouse.BillPagersAdapter;
import com.wwwarehouse.financialcenter.bean.bill.BillRecordResponseBean;
import com.wwwarehouse.financialcenter.bean.bill.SerializableMap;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;

import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/11/14.
 *         <p/>
 *         账单子类界面
 *
 */

public class BillChildFragment extends TheParentFragment implements BillPagersAdapter.OnItemClickListener {
    private RecyclerView mRecyclerView;
    private BillPagersAdapter mAdapter;

    private String mBusinessId;
    private int mHeight;
    private List<BillRecordResponseBean.BillRecordDetailsBean> mBillRecordDetailsList;

    public static final int REQUEST_GET_BILL_RECODE = 1;
    private boolean isFunction  = false; //是否是从功能卡进入
    @Override
    protected int getContentId() {
        return R.layout.financial_center_bill_fragment_recycler_pagers;
    }

    @Override
    protected void init(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_content);
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mBusinessId = bundle.getString("businessId");
            isFunction = bundle.getBoolean("isFunction");

            SerializableMap serializableMap = (SerializableMap) bundle.getSerializable("map");
            Map map = serializableMap.getMap();

            int pos = bundle.getInt("pos");
            int num = bundle.getInt("num");

            map.put("page", pos);
            map.put("size", num);

            httpPost(FinancialConstant.URL_GET_BILL_RECORD, map, REQUEST_GET_BILL_RECODE, true, null);
        }
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case REQUEST_GET_BILL_RECODE:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    final BillRecordResponseBean billRecordResponseBean = JSON.parseObject(commonClass.getData().toString(), BillRecordResponseBean.class);
                    mBillRecordDetailsList = billRecordResponseBean.getList();

                    mRecyclerView.addItemDecoration(new SpaceItemDecoration(ConvertUtils.dip2px(mActivity, 5), ConvertUtils.dip2px(mActivity, 5)));
                    mRecyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));

                    final ViewTreeObserver observer = mRecyclerView.getViewTreeObserver();

                    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            observer.removeGlobalOnLayoutListener(this);

                            mHeight = mRecyclerView.getHeight();
                            mAdapter = new BillPagersAdapter(mActivity, mBillRecordDetailsList, mHeight);
                            mRecyclerView.setAdapter(mAdapter);
                            mAdapter.setOnItemClickListener(BillChildFragment.this);
                        }
                    });
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void ItemClickListener(int position, View view, String data) {
        ((BaseCardDeskActivity) mActivity).recoveryTitleBtStatus_4();
        ((BaseCardDeskActivity) mActivity).hideSearchInput();
        ((BaseCardDeskActivity) mActivity).hideSearchTitle();

        BillRecordResponseBean.BillRecordDetailsBean billRecordDetailsBean = mBillRecordDetailsList.get(position);
        billRecordDetailsBean.setBusinessId(mBusinessId);

        Bundle bundle = new Bundle();
        bundle.putParcelable("data", billRecordDetailsBean);
        bundle.putBoolean("isFunction",isFunction);
        BillDetailsFragment billDetailsFragment = new BillDetailsFragment();
        billDetailsFragment.setArguments(bundle);
        pushFragment(billDetailsFragment);
    }
}
